# Sources:
CPP_SRC=hd44780fw_cppwrapper.cpp
HPP_SRC=hd44780fw_cppwrapper.hpp

# Libs dependency
MY_LIBS = hd44780fw customprocs hd44780

# Target MCU
MCU ?= atmega16
F_CPU ?= 8000000UL

# Target library:
TARGET_LIB = libhd44780fw_cppwrapper.a

# Additional includes:
INC=-I../avrutils $(addprefix -I../lib,$(MY_LIBS))

# Release configurations:
RELNAME=libhd44780fw_cppwrapper-$(RELVER)
RELDIR=../../tags/$(RELNAME)
RELFILES=$(CPP_SRC) $(HPP_SRC) README COPYING

# Configuration:
HEXFORMAT=ihex
OPTLEVEL=s

# Tools:
CC=avr-gcc
OBJCOPY=avr-objcopy
OBJDUMP=avr-objdump
SIZE=avr-size
RM=rm -f
AR=avr-ar
CP=cp

# Shorts:
O_FILES=$(subst .cpp,.o,$(CPP_SRC))

# C flags:
CFLAGS=-I. $(INC) -g -mmcu=$(MCU) -O$(OPTLEVEL) \
	-fpack-struct -fshort-enums \
	-funsigned-bitfields -funsigned-char \
	-Wall \
	-DF_CPU=$(F_CPU)

# C++ flags:
CPPFLAGS=-fno-exceptions -std=c++98

# Archiver flags:
ARFLAGS=-rcs

# Phony targets:
.PHONY: clean release

# Archive target:
$(TARGET_LIB): $(O_FILES) $(HPP_SRC)
	$(AR) $(ARFLAGS) $@ $(O_FILES)	

# Objects from C files:
%.o : %.cpp
	$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<

# Clean target:
clean:
	$(RM) $(TARGET_LIB) $(O_FILES)

# Tag current state and make a release:
release:
	make	# Be sure there are no errors...
	if [ ! -d $(RELDIR) ]; then mkdir $(RELDIR); fi
	$(CP) $(RELFILES) $(RELDIR)
	$(CP) Makefile.release $(RELDIR)/Makefile
	cd ../../tags && tar -cvzf $(RELNAME).tgz $(RELNAME)
	mv ../../tags/$(RELNAME).tgz ../../tags/tarballs

